/*
 * @Author: chengsl
 * @Date: 2023-05-08 11:35:18
 * @LastEditors: chengsl
 * @LastEditTime: 2023-05-08 11:39:29
 * @Description: file content
 */

const user = {
  state: {
    token: '',
    user: {}, // 用户信息
    roles: [], // 用户所具有的角色
    // 第一次加载菜单时用到
    loadMenus: false,
    sysList: []
  },

  mutations: {
    SET_SYS_LIST: (state, sysList) => {
      state.sysList = sysList
    },
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_USER: (state, user) => {
      state.user = user
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_LOAD_MENUS: (state, loadMenus) => {
      state.loadMenus = loadMenus
    }
  },

  actions: {
    // 登录
    Login({ commit }, userInfo) {
      commit('SET_TOKEN', '')
      !userInfo && console.log(userInfo)
    },

    // 获取用户信息
    GetInfo({ commit }) {
      !commit && console.log(commit)
    },
    // 登出
    LogOut({ commit }) {
      !commit && console.log(commit)
    },

    updateLoadMenus({ commit }) {
      return new Promise(() => {
        commit('SET_LOAD_MENUS', false)
      })
    }
  }
}

export const logOut = (commit) => {
  commit('SET_TOKEN', '')
  commit('SET_ROLES', [])
  // removeToken()
}

export const setUserInfo = (res, commit) => {
  commit('SET_ROLES', res.data.roles)
  commit('SET_USER', res.data)
}

export default user
